
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from .models import Rank
from .serializer import RankSerializer


class RankViewSet(viewsets.ModelViewSet):
    queryset = Rank.objects.all()
    serializer_class = RankSerializer

    def perform_create(self, serializer):
        serializer.save()

    @action(detail=False, methods=["get"], url_path="api/(?P<value>[^/.]+)")
    def select(self, request, value):
        # 获取 URL 参数
        filter_dict = dict(self.request.query_params.items())
        list_value = self.kwargs.get("value").split(',')
        queryset = Rank.objects.filter(**filter_dict).values(*list_value, "id")
        return Response( {"results": queryset})

